﻿using System.Collections.Generic;
using FileHelpers;
using Rhino.Etl.Core;
using Rhino.Etl.Core.Files;
using Rhino.Etl.Core.Operations;

namespace Etl.Operations {
    public class ToSingleColumnFile : AbstractOperation     {

        private readonly string _fileName;
        private readonly string _column;
        private readonly string _header;
        private readonly string _footer;

        public ToSingleColumnFile(string fileName, string column, string header = "", string footer = "") {
            _fileName = fileName;
            _column = column;
            _header = header;
            _footer = footer;
        }

        [DelimitedRecord(",")] 
        public class Line {
            public string Column;
        }

        public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
        {
            var engine = FluentFile.For<Line>();
            engine.HeaderText = _header;
            engine.FooterText = _footer;
            using (var file = engine.To(_fileName)) {
                foreach (var row in rows) {
                    file.Write(new Line {Column = row[_column].ToString()});
                }
            }
            yield break;
        }


    }
}
